#include "ezvoiceplayer.h"
#include <QUrl>
#include <QNetworkRequest>
#include <QMediaContent>
#include "decode/libavdecode.h"
#include "lib/stream/filestream.h"
#include "player/ezqaudioplayer.h"

#include "model/commonModel/songmodel.h"

EZVoicePlayer* EZVoicePlayer::defaultPlayer()
{
    static EZVoicePlayer *player = 0;
    if(player==0){
        player = new EZVoicePlayer(0);
    }
    return player;
}

EZVoicePlayer::EZVoicePlayer(QObject *parent) :
    QObject(parent)
{
    state = EZVoiceState(EZVoicePlayerState_stop,EZVoiceMediaState_noMedia,0);
    song = 0;
    connect(&mediaPlayer,SIGNAL(mediaStatusChanged(QMediaPlayer::MediaStatus)),this,SLOT(onMediaStateChange(QMediaPlayer::MediaStatus)));
    connect(&mediaPlayer,SIGNAL(stateChanged(QMediaPlayer::State)),this,SLOT(onStateChanged(QMediaPlayer::State)));
    connect(&mediaPlayer,SIGNAL(bufferStatusChanged(int)),this,SLOT(onBufferStatusChanged(int)));
}

EZVoicePlayer::~EZVoicePlayer()
{
    if(this->song)
        this->song->Release();
}


void EZVoicePlayer::playSong(SongModel *song)
{
//    LibavDecode *decode = new LibavDecode();
    QString path = song->getUrlWith(songHash_localPath,0);
    FileStream *fileStream = new FileStream(path);
//    decode->setStream(fileStream);
//    decode->probe();
//    decode->probeInfo();
//    decode->decode();

    EZQAudioPlayer *player = EZQAudioPlayer::sharePlayer();
    player->setStream(fileStream);
    player->play();

    return;
    EZVoiceState newState;
    newState.playState = EZVoicePlayerState_plyaing;
    newState.mediaState = EZVoiceMediaState_pres;
    newState.subMediaState = EZVoiceStatePres_start;
    setSong(song);
    stop();//停止之前的播放
//    QString path = song->getUrlWith(songHash_localPath,0);
    if(path.isEmpty()==false){ // local
        QMediaContent mediaContent;

        QUrl url = QUrl(QString("file://")+path);
        mediaContent = QMediaContent(url);


        mediaPlayer.setMedia(mediaContent);
        play();
    }else{
        QString key(songHash_320);
        int getType = 0;
        QString strUrl = song->getUrlWith(key,&getType);
        if(getType<0){
            key = QString(songHash_normal);
            strUrl = song->getUrlWith(key,&getType);
        }

        if(getType<0){
            qDebug() <<"not path and no hash can not player";
        }else{
            if(strUrl.isEmpty()==false){
                QMediaContent mediaContent;
                QUrl url = QUrl(strUrl);
                QNetworkRequest request(url);
                mediaContent = QMediaContent(request);
                mediaPlayer.setMedia(mediaContent);
                play();
            }else{
                //wait get url on net will player
            }
        }

    }
    setPlayState(newState);

}

void EZVoicePlayer::playOrPause()
{
    if(mediaPlayer.state()==QMediaPlayer::PlayingState){
        pause();
    }else{
        play();
    }
}

void EZVoicePlayer::play()
{
    mediaPlayer.play();
}

void EZVoicePlayer::pause()
{
    mediaPlayer.pause();
}

void EZVoicePlayer::stop()
{
    mediaPlayer.stop();
}
qint64 EZVoicePlayer::duration()
{
    return mediaPlayer.duration();
}
qint64 EZVoicePlayer::currentTime()
{
    return mediaPlayer.position();
}

void EZVoicePlayer::setCurrentTime(qint64 time)
{
    mediaPlayer.setPosition(time);
}

int EZVoicePlayer::volume()
{
    return mediaPlayer.volume();
}

void EZVoicePlayer::setVolume(int volume)
{
    mediaPlayer.setVolume(volume);
}

void EZVoicePlayer::setSong(SongModel *song_)
{
    if(song_){
        connect(song_,SIGNAL(sigSongUrlChange(QString&,int)),this,SLOT(OnSongGetUrl(QString&,int)));
        song_->Retain();
    }
    if(this->song){
        disconnect(song,SIGNAL(sigSongUrlChange(QString&,int)),this,SLOT(OnSongGetUrl(QString&,int)));
        this->song->Release();
    }
    this->song = song_;
}
SongModel* EZVoicePlayer::getSong()
{
    return song;
}

void EZVoicePlayer::OnSongGetUrl(QString &key,int error)
{
    if(error){
        qDebug() << "error get song url finis bug error";
        return;
    }
    SongModel::Quality quality = song->qualityWithKey(key);
    if(quality.url.isEmpty()){
        qDebug() << "error get song url finis bug error url is null";
        return;
    }
    if(state.playState ==EZVoicePlayerState_plyaing){
        qDebug() << mediaPlayer.PlayingState;
        if(mediaPlayer.state() == QMediaPlayer::StoppedState ){
            QUrl url = QUrl(quality.url);
            QNetworkRequest request(url);
            QMediaContent mediaContent = QMediaContent(request);
            mediaPlayer.setMedia(mediaContent);
            play();
        }
    }

}

void EZVoicePlayer::setPlayState(EZVoiceState newState)
{
    state = newState;
    emit sigstateChanged(state);
}

EZVoiceState EZVoicePlayer::MediaBufferStateToMyMediaState(QMediaPlayer::MediaStatus mmstatus)
{
    EZVoiceState newState = state;
    switch(mmstatus){
        case QMediaPlayer::UnknownMediaStatus:
    {
        newState.mediaState = EZVoiceMediaState_pres;
    }
            break;
        case QMediaPlayer::NoMedia:
        newState.mediaState = EZVoiceMediaState_noMedia;
            break;
        case QMediaPlayer::LoadingMedia:
        case QMediaPlayer::LoadedMedia:
        newState.mediaState = EZVoiceMediaState_pres;
        newState.subMediaState = EZVoiceStatePres_ing;
            break;
        case QMediaPlayer::StalledMedia:
            break;
        case QMediaPlayer::BufferingMedia:
        newState.mediaState = EZVoiceMediaState_buffs;
        newState.subMediaState = EZVoiceStatePres_ing;
            break;
        case QMediaPlayer::BufferedMedia:
        newState.mediaState = EZVoiceMediaState_buffs;
        newState.subMediaState = EZVoiceStatePres_end;
            break;
        case QMediaPlayer::EndOfMedia:
        newState.mediaState = EZVoiceMediaState_end;
        newState.subMediaState = 0;
            break;
        case QMediaPlayer::InvalidMedia:
        newState.mediaState = EZVoiceMediaState_error;
        newState.subMediaState = 0;
            break;
    }
    return newState;
}

//! [QMediaplayer slots]
void EZVoicePlayer::onStateChanged(QMediaPlayer::State mstate)
{
    EZVoiceState newState = state;
    switch(mstate){
    case QMediaPlayer::StoppedState:
        newState.playState = EZVoicePlayerState_stop;
        break;
    case QMediaPlayer::PlayingState:
        newState.playState = EZVoicePlayerState_plyaing;
        break;
    case QMediaPlayer::PausedState:
        newState.playState = EZVoicePlayerState_pause;
        break;
    }
    setPlayState(newState);
//    state = newState;
//    emit stateChanged(this,state);
//    qDebug() << "playstateChange";
}
void EZVoicePlayer::onMediaStateChange(QMediaPlayer::MediaStatus status)
{
    qDebug() << "QMediaPlayer::MediaStatus:" << status;
    EZVoiceState newState = MediaBufferStateToMyMediaState(status);
    setPlayState(newState);
}

void EZVoicePlayer::onBufferStatusChanged(int percentFilled)
{
    qDebug() << "percentFilled:"<<percentFilled;
}

//! [QMediaplayer slots]
